Skip to content

plotting

plotting

SingleAxisFigure dataclass

SingleAxisFigure(tag: Tag, fig: Figure, ax: Axes)

Data class storing a matlab figure and axis. The stored tag data in this class is so-far unused.

Attributes:

Name Type Description
ax Axes

Matplotlib axis to which data will be plotted

fig Figure

Matplotlib figure.

tag Tag

Figure tag. Not yet used.

__del__

__del__()

Closes stored matplotlib figure before deleting reference to object.

Source code in src/trendify/api/plotting/plotting.py
def __del__(self):
    """
    Closes stored matplotlib figure before deleting reference to object.
    """
    plt.close(self.fig)

apply_format

apply_format(format2d: Format2D)

Applies format to figure and axes labels and limits

Parameters:

Name Type Description Default
format2d Format2D

format information to apply to the single axis figure

required
Source code in src/trendify/api/plotting/plotting.py
def apply_format(self, format2d: Format2D):
    """
    Applies format to figure and axes labels and limits

    Args:
        format2d (Format2D): format information to apply to the single axis figure
    """
    if format2d.title_ax is not None:
        self.ax.set_title(format2d.title_ax)
    if format2d.title_fig is not None:
        self.fig.suptitle(format2d.title_fig)

    leg = None
    if format2d.legend is not None:
        with warnings.catch_warnings(action="ignore", category=UserWarning):
            handles, labels = self.ax.get_legend_handles_labels()
            by_label = dict(zip(labels, handles))
            if by_label:
                sorted_items = sorted(by_label.items(), key=lambda item: item[0])
                labels_sorted, handles_sorted = zip(*sorted_items)

                kwargs = format2d.legend.to_kwargs()

                leg = self.ax.legend(
                    handles=handles_sorted,
                    labels=labels_sorted,
                    bbox_to_anchor=format2d.legend.bbox_to_anchor,
                    **kwargs,
                )

                if leg is not None and format2d.legend.edgecolor:
                    leg.get_frame().set_edgecolor(format2d.legend.edgecolor)

    if format2d.label_x is not None:
        self.ax.set_xlabel(xlabel=format2d.label_x)
    if format2d.label_y is not None:
        self.ax.set_ylabel(ylabel=format2d.label_y)

    self.ax.set_xlim(left=format2d.lim_x_min, right=format2d.lim_x_max)
    self.ax.set_ylim(bottom=format2d.lim_y_min, top=format2d.lim_y_max)

    self.ax.set_xscale(format2d.scale_x.value)
    self.ax.set_yscale(format2d.scale_y.value)

    if format2d.grid is not None:
        self.apply_grid(format2d.grid)

    self.fig.tight_layout(rect=(0, 0.03, 1, 0.95))
    return self

new classmethod

new(tag: Tag)

Creates new figure and axis. Returns new instance of this class.

Parameters:

Name Type Description Default
tag Tag

tag (not yet used)

required

Returns:

Type Description
Type[Self]

New single axis figure

Source code in src/trendify/api/plotting/plotting.py
@classmethod
def new(cls, tag: Tag):
    """
    Creates new figure and axis.  Returns new instance of this class.

    Args:
        tag (Tag): tag (not yet used)

    Returns:
        (Type[Self]): New single axis figure
    """
    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    return cls(
        tag=tag,
        fig=fig,
        ax=ax,
    )

savefig

savefig(path: Path, dpi: int = 500)

Wrapper on matplotlib savefig method. Saves figure to given path with given dpi resolution.

Returns:

Type Description
Self

Returns self

Source code in src/trendify/api/plotting/plotting.py
def savefig(self, path: Path, dpi: int = 500):
    """
    Wrapper on matplotlib savefig method.  Saves figure to given path with given dpi resolution.

    Returns:
        (Self): Returns self
    """
    self.fig.savefig(path, dpi=dpi)
    return self